package com.santoker.user.service.controller;

import com.santoker.baseservice.ApiResponseBody;
import com.santoker.baseservice.ApiResponseCode;
import com.santoker.baseservice.redis.AuthInfo;
import com.santoker.user.service.dto.vo.auth.*;
import com.santoker.user.service.dto.vo.user.UserInfoModifyVo;
import com.santoker.user.service.service.biz.auth.AuthService;
import com.santoker.user.service.service.biz.message.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Objects;

@Api(tags = "注册登陆相关api")
@RestController
@RequestMapping("/painters/apis")
public class AuthController extends BaseController {

    @Autowired
    private MessageService messageService;
    @Autowired
    private AuthService authService;

    @ApiOperation(value = "获取国内短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_captcha"})
    public ApiResponseBody getCaptcha(
            @RequestParam(value = "mobile", required = true) String mobile
    ) {
        messageService.sendCaptchaMessage(mobile);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "获取国际短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_internation_captcha"})
    public ApiResponseBody getInternetionCaptcha(
            @RequestParam(value = "mobile", required = true) String mobile
    ) {
        messageService.sendInternetionCaptchaMessage(mobile);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "获取邮箱验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "邮箱", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_email_captcha"})
    public ApiResponseBody getEmailCaptcha(
            @RequestParam(value = "email", required = true) String email
    ) {
        messageService.sendEmailCaptchaMessage(email);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "手机号注册")
    @PostMapping(value = {"/register"})
    @ResponseBody
    public ApiResponseBody<RegisterResponseVo> register(
            @Valid @RequestBody RegisterVo body, BindingResult result
    ) {
        validParams(result);
        RegisterResponseVo registerResponseVo = authService.register(body);
        return ApiResponseBody.createSuccessBody(registerResponseVo);
    }

    @ApiOperation(value = "邮箱注册")
    @PostMapping(value = {"/email_register"})
    @ResponseBody
    public ApiResponseBody<RegisterResponseVo> email_register(
            @Valid @RequestBody EmailRegisterVo body, BindingResult result
    ) {
        validParams(result);
        RegisterResponseVo registerResponseVo = authService.emailRegister(body);
        return ApiResponseBody.createSuccessBody(registerResponseVo);
    }

    @ApiOperation(value = "手机号登录", notes = "sid不同时会重新登陆，cid用于绑定到个推")
    @PostMapping(value = {"/login"})
    @ResponseBody
    public ApiResponseBody<LoginResponseVo> mobileLogin(
            @Valid @RequestBody LoginVo body, BindingResult result
    ) {
        validParams(result);
        LoginResponseVo loginResponseVo = authService.mobileLogin(body);
        return ApiResponseBody.createSuccessBody(loginResponseVo);
    }

    @ApiOperation(value = "邮箱登录", notes = "sid不同时会重新登陆，cid用于绑定到个推")
    @PostMapping(value = {"/login_by_email"})
    @ResponseBody
    public ApiResponseBody<LoginResponseVo> loginByEmail(
            @Valid @RequestBody LoginVo body, BindingResult result
    ) {
        validParams(result);
        LoginResponseVo loginResponseVo = authService.emailLogin(body);
        return ApiResponseBody.createSuccessBody(loginResponseVo);
    }

    @ApiOperation(value = "重置密码")
    @PostMapping(value = {"/reset"})
    @ResponseBody
    public ApiResponseBody resetPassword(
            @Valid @RequestBody ResetVo body, BindingResult result
    ) {
        validParams(result);
        authService.resetPassword(body);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "微信授权登录（已作废）", notes = "sid不同时会重新登陆，cid用于绑定到个推")
    @PostMapping(value = {"/wx_login"})
    @ResponseBody
    public ApiResponseBody<LoginResponseVo> wxLogin(
            @Valid @RequestBody WxLoginVo body, BindingResult result
    ) {
        validParams(result);
        LoginResponseVo loginResponseVo = authService.wxLogin(body);
        return ApiResponseBody.createSuccessBody(loginResponseVo);
    }

    @ApiOperation(value = "新微信授权登录", notes = "sid不同时会重新登陆，cid用于绑定到个推")
    @PostMapping(value = {"/v2/wx_login"})
    @ResponseBody
    public ApiResponseBody<LoginResponseVo> wxLoginV2(
            @Valid @RequestBody WxLoginVo body, BindingResult result
    ) {
        validParams(result);
        LoginResponseVo loginResponseVo = authService.wxLoginV2(body);
        if (Objects.isNull(loginResponseVo.getId())) {
            return ApiResponseBody.createErrorBody(ApiResponseCode.NOT_BIND_ACCOUNT, loginResponseVo);
        }
        return ApiResponseBody.createSuccessBody(loginResponseVo);
    }

    @ApiOperation(value = "微信绑定")
    @PostMapping(value = {"/wx_bind"})
    @ResponseBody
    public ApiResponseBody<LoginResponseVo> wxBind(
            @Valid @RequestBody WxBindVo body, BindingResult result
    ) {
        validParams(result);
        LoginResponseVo loginResponseVo = authService.wxBind(body);
        return ApiResponseBody.createSuccessBody(loginResponseVo);
    }

    @ApiOperation(value = "获取账号验证码")
    @PostMapping(value = {"/get_account_captcha"})
    @ResponseBody
    public ApiResponseBody<LoginResponseVo> getAccountCaptcha(
            @Valid @RequestBody GetAccountCaptchaVo body, BindingResult result
    ) {
        validParams(result);
        messageService.getAccountCaptcha(body.getAccountType(), body.getAccount());
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "微信解绑")
    @PostMapping(value = {"/wx_unbind"})
    @ResponseBody
    public ApiResponseBody<LoginResponseVo> wxUnbind(
            HttpServletRequest request
    ) {
        AuthInfo authInfo = getAuthInfo(request);
        authService.wxUnbind(authInfo);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "修改用户信息")
    @PostMapping(value = {"/modify"})
    @ResponseBody
    public ApiResponseBody<UserInfoModifyVo> modifyUser(
            @Valid @RequestBody UserInfoModifyVo body, BindingResult result,
            HttpServletRequest request
    ) {
        validParams(result);
        AuthInfo authInfo = getAuthInfo(request);
        authService.modifyUser(authInfo.getId(), body);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "登出")
    @ApiImplicitParams({})
    @GetMapping(value = {"/logout"})
    public ApiResponseBody logout(
            HttpServletRequest request
    ) {
        AuthInfo authInfo = getAuthInfo(request);
        authService.logout(authInfo);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "微信绑定当前登录账号")
    @PostMapping(value = {"/wx_bind2"})
    @ResponseBody
    public ApiResponseBody<LoginResponseVo> wxBind2(
            @Valid @RequestBody WxBind2Vo body, BindingResult result,
            HttpServletRequest request
    ) {
        validParams(result);
        AuthInfo authInfo = getAuthInfo(request);
        LoginResponseVo loginResponseVo = authService.wxBind2(body, authInfo);
        return ApiResponseBody.createSuccessBody(loginResponseVo);
    }

    @ApiOperation(value = "手机号/邮箱绑定")
    @PostMapping(value = {"/mobile_email_bind"})
    @ResponseBody
    public ApiResponseBody<LoginResponseVo> mobileEmailBind(
            @Valid @RequestBody MobileEmailBindVo body, BindingResult result,
            HttpServletRequest request
    ) {
        validParams(result);
        AuthInfo authInfo = getAuthInfo(request);
        LoginResponseVo loginResponseVo = authService.mobileEmailBind(body, authInfo);
        return ApiResponseBody.createSuccessBody(loginResponseVo);
    }

    @ApiOperation(value = "固件更新")
    @PostMapping(value = {"/firmware_update"})
    @ResponseBody
    public ApiResponseBody<VersionVo> firmwareUpdate(
            @Valid @RequestBody FirmwareUpdateVo body, BindingResult result
    ) {
        validParams(result);
        VersionVo versionVo = authService.firmwareUpdate(body);
        return ApiResponseBody.createSuccessBody(versionVo);
    }

}
